********************************************************************************
**** 				CREATING MARGINS GRAPH   

* File description: Creating figure 1
********************************************************************************

clear all
set more off

use "~/use/final_data", clear


* Figure 1a+b				                     

global controls age age2 female educ married kids unemployed
global area_controls avg_age share_poor share_loweduc share_selfemployed share_female avg_kids share_married lnp50 lnp20 lnhouseprice population

*Figure 1(a)

areg bf_dummy percentile_2lag##c.yitz_muni lndispinc $controls $area_controls i.year, cluster(municipality) a(municipality) robust

margins, dydx(yitz_muni) at(percentile_2lag=(1(1)10))


marginsplot, recast(line) ciopt(color(black%20)) ///
recastci(rarea) title("AVG") yti("Effect on Claims") ///
graphregion(color(white)) name("diff", replace)

graph save "~/out/figure1_yitz_percentiles", replace



*Figure 1(b)

egen mean_bf_2lag=mean(bf_dummy), by(percentile_2lag)
egen mean_bf=mean(bf_dummy), by(percentile)


sum bf_dummy, d
local mean=`r(mean)'

di `mean'


keep mean_bf_2lag percentile_2lag
duplicates drop 
label var mean_bf_2lag "Share with claims"
label var percentile_2lag "Percentile"

sort percentile_2lag


sum mean_bf_2lag
local mean=round(`r(mean)',0.0001)
local mean2=`mean'+0.002


di `mean'
di `mean2'

twoway (connect mean_bf_2lag percentile_2lag, color(gray)), graphregion(color(white)) xl(1(1)10) yline(`mean') text(`mean2' 9 "Population mean=`mean'", size(.3cm)) yl(0(0.01)0.07)

graph save "~/out/mean_bf_dummy_percentiles2lag", replace



